package com.mercycrazy.recruit.controller;

import com.mercycrazy.recruit.user.MobileAndCode;
import com.mercycrazy.recruit.user.UserLoginInfo;
import com.mercycrazy.recruit.service.IUserAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 对 APP 端暴露的鉴权微服务的相关接口
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2023-01-14
 */
@RestController
@RequestMapping("/passport/app")
@Slf4j
@Tag(name = "APP鉴权")
public class AppPassportController {

    private final IUserAuthService userAuthService;

    public AppPassportController(IUserAuthService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @Operation(summary = "获取短信验证码",
            description = "短信验证码登录, 30分钟内有效, 60秒内只能发送一次", method = "POST")
    @PostMapping("/sms-code")
    public void sendSMSCode(@RequestBody @Valid MobileAndCode mobileAndCode) {
        userAuthService.sendSMSCode(mobileAndCode.getMobile(), 30L);
    }

    @Operation(summary = "一键注册登录",
            description = "手机号验证码一键注册登录, 返回 token 字符串", method = "POST")
    @PostMapping("/login")
    public UserLoginInfo login(@RequestBody @Valid MobileAndCode mobileAndCode) {
        return userAuthService.loginAndGenerateToken(mobileAndCode);
    }

    @Operation(summary = "APP端扫描二维码登录企业端",
            description = "APP端扫描二维码登录企业端, 返回 preToken 字符串, 有效期5分钟, 需要登录才能操作", method = "GET")
    @GetMapping("/scan-code")
    public String scanCode(@RequestParam String qrToken) {
        return userAuthService.scanCode(qrToken);
    }

    @Operation(summary = "APP端确认登录企业端",
            description = "APP端确认登录企业端, 保存临时用户信息到redis, 需要登录才能操作", method = "GET")
    @GetMapping("/confirm-saas-login")
    public void confirmSaasLogin(@RequestParam String qrToken,
                                 @RequestParam String preToken) {
        userAuthService.confirmSaasLogin(qrToken, preToken);
    }
}
